package cn.hl.sfzy.service.impl;

import cn.hl.sfzy.entity.Article;
import cn.hl.sfzy.entity.Consult;
import cn.hl.sfzy.entity.Course;
import cn.hl.sfzy.entity.CourseType;
import cn.hl.sfzy.mapper.ArticleMapper;
import cn.hl.sfzy.mapper.ConsultMapper;
import cn.hl.sfzy.mapper.CourseMapper;
import cn.hl.sfzy.mapper.CourseTypeMapper;
import cn.hl.sfzy.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO
 * @Author libowen
 * @Date 2024年10月25日 10:25
 * @Version 1.0
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CourseTypeMapper courseTypeMapper;

    @Autowired
    private ConsultMapper consultMapper;

    @Override
    public Course findById(Long id) {
        Course course = courseMapper.selectById(id);
        return course;
    }

    @Override
    public int deleteCourse(Long id) {
        int delete = courseMapper.delete(id);
        return delete;
    }

    @Override
    public int addCourse(Course course) {
        int insert = courseMapper.insert(course);
        return insert;
    }

    @Override
    public List<Course> getCourses(Course course) {
        List<Course> select = courseMapper.select(course);
        return select;
    }
    @Override
    public int updateCourse(Course course) {
        int update = courseMapper.update(course);
        return update;
    }

    @Override
    public PageInfo<Course> findByPage(int pageNum, int pageSize, Course course) {
        PageHelper.startPage(pageNum, pageSize);
        List<Course> list = courseMapper.select(course);
        // 遍历课程内容，并根据课程内容中的typeId来查询对应的文章类型对象
        for(Course cs : list){
            CourseType courseType =
                    courseTypeMapper.selectById(cs.getType_id());
            cs.setCourseType(courseType);
        }
        for(Course cs : list){
            Consult consult =
                    consultMapper.selectById(cs.getTeacher_id());
            cs.setConsult(consult);
        }
        PageInfo<Course> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public PageInfo<Course> selectByConsultId( int pageNum, int pageSize, Long teacher_id) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Course> info = new PageInfo<>(courseMapper.selectByConsultId(teacher_id));
        return info;
    }
}
